package com.czkt.config;

import com.czkt.pojo.SysRight;
import com.czkt.service.ISysRightService;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Autowired
    ISysRightService rightService;

    //加密方式
    @Bean
    public CredentialsMatcher getHashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //加密次数
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

    @Bean
    public MyShiroRealm shiroRealm(){
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        //开启缓存
        myShiroRealm.setAuthorizationCachingEnabled(true);
        myShiroRealm.setAuthorizationCacheName("authorizationCacheName");
        //设置加密方式
        myShiroRealm.setCredentialsMatcher(getHashedCredentialsMatcher());
        return myShiroRealm;
    }

    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(shiroRealm());
        securityManager.setCacheManager(redisCacheManager());
        securityManager.setSessionManager(defaultWebSessionManager());
        return securityManager;
    }

    public RedisManager redisManager(){
        RedisManager redisManager = new RedisManager();
        return redisManager;
    }

    public RedisCacheManager redisCacheManager(){
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());

        redisCacheManager.setPrincipalIdFieldName("usrId");
        return redisCacheManager;
    }

    public RedisSessionDAO redisSessionDAO(){
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(redisManager());
        return redisSessionDAO;
    }

    public DefaultWebSessionManager defaultWebSessionManager(){
        DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
        defaultWebSessionManager.setSessionDAO(redisSessionDAO());
        //URL不显示SessionId
        defaultWebSessionManager.setSessionIdUrlRewritingEnabled(false);
        return defaultWebSessionManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
        shiroFilterFactory.setSecurityManager(securityManager);
        shiroFilterFactory.setLoginUrl("/login");
        shiroFilterFactory.setSuccessUrl("/main");
        shiroFilterFactory.setUnauthorizedUrl("/403");
        //配置规则
        Map<String,String> urlMap = new LinkedHashMap<>();
        urlMap.put("/css/*","anon");
        urlMap.put("/js/*","anon");
        urlMap.put("/fonts/*","anon");
        urlMap.put("/images/*","anon");
        urlMap.put("/localcss/*","anon");
        urlMap.put("/localjs/*","anon");

        urlMap.put("/logout","anon");

        urlMap.put("/doLogin","anon");
        urlMap.put("/login","anon");

        //从数据库获取
        List<SysRight> rightList = rightService.list();
        for (SysRight sysRight : rightList) {
            if (sysRight.getRightUrl() != null && sysRight.getRightUrl().isEmpty()){
                urlMap.put(sysRight.getRightUrl(),"perms[" + sysRight.getRightCode() + "]");
            }
        }

        urlMap.put("/**","authc");
        shiroFilterFactory.setFilterChainDefinitionMap(urlMap);
        return shiroFilterFactory;
    }
}